namespace SmartSoft.ADP
{
    using System;
    using System.Data;
    using System.Data.SqlClient;

    public class CTVATDataAccess
    {
        private WSCTVATDataAccess.WSCTVATDataAccess wDataAcess = new WSCTVATDataAccess.WSCTVATDataAccess();
        private SmartSoft.DAL.CTVATDataAccess oDataAcess = new SmartSoft.DAL.CTVATDataAccess();

        public CTVATDataAccess()
        {
            wDataAcess.Url = wDataAcess.Url.Replace("localhost", AdapterInstance.ServerIp);
        }

        public int CheckInvoiceLock(string ps_Subsidiary, string ps_UserName, object ps_InvoiceOrder)
        {
            if (AdapterInstance.IsRemoteConnected)
            {
                return wDataAcess.CheckInvoiceLock(ps_Subsidiary, ps_UserName, ps_InvoiceOrder);
            }
            else
            {
                return oDataAcess.CheckInvoiceLock(ps_Subsidiary, ps_UserName, ps_InvoiceOrder);
            }
        }

        public int CheckInvoiceStatus(string ps_Subsidiary, string ps_UserName, object ps_InvoiceOrder)
        {
            if (AdapterInstance.IsRemoteConnected)
            {
                return wDataAcess.CheckInvoiceStatus(ps_Subsidiary, ps_UserName, ps_InvoiceOrder);
            }
            else
            {
                return oDataAcess.CheckInvoiceStatus(ps_Subsidiary, ps_UserName, ps_InvoiceOrder);
            }
        }

        public bool Delete(string ps_UserName, object po_KeyCode)
        {
            if (AdapterInstance.IsRemoteConnected)
            {
                return wDataAcess.Delete(ps_UserName, po_KeyCode == DBNull.Value ? string.Empty : po_KeyCode);
            }
            else
            {
                return oDataAcess.Delete(ps_UserName, po_KeyCode);
            }
        }

        public DataRow GetDataRow(string ps_InvoiceOrder)
        {
            if (AdapterInstance.IsRemoteConnected)
            {
                return SmartSoft.DAL.SSDataAccess.ConvertTableToRow(wDataAcess.GetDataRow(ps_InvoiceOrder));
            }
            else
            {
                return oDataAcess.GetDataRow(ps_InvoiceOrder);
            }
        }

        public DataTable GetTableByMaster(string ps_MasterInvoiceOrder)
        {
            DataTable table = null;
            if (AdapterInstance.IsRemoteConnected)
            {
                table= wDataAcess.GetTableByMaster(ps_MasterInvoiceOrder);
            }
            else
            {
                table= oDataAcess.GetTableByMaster(ps_MasterInvoiceOrder);
            }
            if (table != null) table.TableName = string.Empty;
            return table;
        }

        public DataTable GetTableByType(string ps_ActiveTransaction)
        {
            DataTable table = null;
            if (AdapterInstance.IsRemoteConnected)
            {
                table= wDataAcess.GetTableByType(ps_ActiveTransaction);
            }
            else
            {
                table= oDataAcess.GetTableByType(ps_ActiveTransaction);
            }
            if (table != null) table.TableName = string.Empty;
            return table;
        }

        public void Lock(string ps_UserName, object ps_InvoiceOrder, bool pb_Locked)
        {
            if (AdapterInstance.IsRemoteConnected)
            {
                wDataAcess.Lock(ps_UserName, ps_InvoiceOrder, pb_Locked);
            }
            else
            {
                oDataAcess.Lock(ps_UserName, ps_InvoiceOrder, pb_Locked);
            }
        }

        public bool NumberExisted(string ps_Subsidiary, bool pb_CheckNewRow, string ps_InvoiceOrder, object ps_So_Ct0, object ps_So_Seri0)
        {
            if (AdapterInstance.IsRemoteConnected)
            {
                return wDataAcess.NumberExisted(ps_Subsidiary, pb_CheckNewRow, ps_InvoiceOrder, ps_So_Ct0, ps_So_Seri0);
            }
            else
            {
                return oDataAcess.NumberExisted(ps_Subsidiary, pb_CheckNewRow, ps_InvoiceOrder, ps_So_Ct0, ps_So_Seri0);
            }
        }

        public bool Save(bool pb_InvoiceHistory, bool pb_NewChecked, string ps_Subsidiary, string ps_UserName, string ps_MasterInvoiceOrder, string ps_Loai_Ps, DataRow po_Row)
        {
            if (AdapterInstance.IsRemoteConnected)
            {
                return wDataAcess.Save(pb_InvoiceHistory, pb_NewChecked, ps_Subsidiary, ps_UserName, ps_MasterInvoiceOrder, ps_Loai_Ps, SmartSoft.DAL.SSDataAccess.ConvertRowToTable(po_Row));
            }
            else
            {
                return oDataAcess.Save(pb_InvoiceHistory, pb_NewChecked, ps_Subsidiary, ps_UserName, ps_MasterInvoiceOrder, ps_Loai_Ps, po_Row);
            }
        }


        public void So_Seri_Get(string ps_Subsidiary, string ps_InvoiceCode, object pd_Ngay_Ct, string ps_InvoiceOrder, ref object ps_So_Seri0, ref object ps_Mau_So0)
        {
            if (AdapterInstance.IsRemoteConnected)
            {
                wDataAcess.So_Seri_Get(ps_Subsidiary, ps_InvoiceCode, pd_Ngay_Ct, ps_InvoiceOrder, ref ps_So_Seri0, ref ps_Mau_So0);
            }
            else
            {
                oDataAcess.So_Seri_Get(ps_Subsidiary, ps_InvoiceCode, pd_Ngay_Ct, ps_InvoiceOrder, ref ps_So_Seri0, ref ps_Mau_So0);
            }
        }
    }
}

